package cn.com.chengmandian.core.lot;

import cn.com.chengmandian.core.lot.properties.AmqpProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@ConditionalOnProperty(prefix = "cmd.iot.amqp", name = "enable", havingValue = "true")
@Component
@Slf4j
public class AmqpRunner implements ApplicationRunner {
    @Autowired
    private AmqpProperties amqpProperties;

    @Autowired
    private AmqpService amqpService;

    @Override
    public void run(ApplicationArguments args) {
        log.info("AmqpRunner running");
        List<String> items = amqpProperties.getConsumerGroupId();
        if (!CollectionUtils.isEmpty(items)) {
            for (String consumerGroupId : items) {
                log.info("AmqpRunner connect {}", consumerGroupId);
                amqpService.createConnections(amqpProperties.getAccessKeyId(),
                        amqpProperties.getAccessKeySecret(),
                        amqpProperties.getHost(),
                        amqpProperties.getIotInstanceId(),
                        amqpProperties.getMaxConnection(),
                        consumerGroupId);
            }
        }
    }
}
